//
//  SelectCompanyViewController.m
//  MyProject
//
//  Created by Pei on 16/7/31.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SelectCompanyViewController.h"
#import "UserInfo.h"
@interface SelectCompanyViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataArray;
    UITableView *_myTable;
    
    NSIndexPath *_lastIndexPath;
    
    //NSMutableArray  *fruitFilterRecordArray;    // 定义全局变量，存储多选页面选中的选项
}

@end

@implementation SelectCompanyViewController

- (void)viewDidLoad {
    self.navTitle = @"公司选择";
    [super viewDidLoad];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    //fruitFilterRecordArray = [[NSMutableArray alloc] initWithCapacity:3];
    
    //_lastIndexPath = nil;
    
    [self initTableView];
    
    [self getData];
}

- (void)rightBtnAcion{
    NSMutableArray *nnnn = [[NSMutableArray alloc] initWithCapacity:3];
    [nnnn removeAllObjects];
    for (NSDictionary *ttttt in _dataArray) {
        if ([[ttttt valueForKey:@"isSelect"] intValue] == 1) {
            [nnnn addObject:ttttt];
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(passCompanyData:)]) {
        [self.delegate passCompanyData:nnnn];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initTableView
{
    _myTable = [[UITableView alloc] init];
    _myTable.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
    
    _myTable.tableFooterView = [UIView new];
}

- (void)getData{
    [[YMNetLib getInstance] connectWithAction:Get_Company_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@", [[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
    {
        _dataArray = [body valueForKey:@"OrgList"];
        NSMutableArray *tttt = [[NSMutableArray alloc] initWithCapacity:3];
        [tttt removeAllObjects];
        for (NSDictionary *dddd in _dataArray) {
            if ([[dddd valueForKey:@"OrgLevel"] intValue] == 0) {
                NSMutableDictionary *yyyy = [[NSMutableDictionary alloc] initWithDictionary:dddd];
                [yyyy setValue:@"0" forKey:@"isSelect"];
                [tttt addObject:yyyy];
            }
            
        }
        _dataArray = tttt;
        [_myTable reloadData];
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell_house_History";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.textColor = kRGB(205, 206, 207);
    cell.textLabel.font = kAppFont(Home_Small_Size+2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [tempDic valueForKey:@"OrgName"];
    if ([[tempDic valueForKey:@"isSelect"] intValue] == 1) {
      cell.accessoryType = UITableViewCellAccessoryCheckmark;//勾标记
    }else{
      cell.accessoryType = UITableViewCellAccessoryNone;//不标记
    }
    //cell.textLabel.text = @"6666666";
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];

    if ([[tempDic valueForKey:@"isSelect"] intValue] == 0) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
        [aaaa setValue:@"1" forKey:@"isSelect"];
        
        NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_dataArray];
        for (int i = 0; i < _dataArray.count; i ++) {
            NSDictionary *rrrr = [_dataArray objectAtIndex:i];
            if ([[rrrr valueForKey:@"OrgCode"] isEqualToString:[aaaa valueForKey:@"OrgCode"]]) {
                [tttttt replaceObjectAtIndex:i withObject:aaaa];
            }
        }
        
        _dataArray = tttttt;
        
    }else{
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryNone;
        
        NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
        [aaaa setValue:@"0" forKey:@"isSelect"];
        
        NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_dataArray];
        for (int i = 0; i < _dataArray.count; i ++) {
            NSDictionary *rrrr = [_dataArray objectAtIndex:i];
            if ([[rrrr valueForKey:@"OrgCode"] isEqualToString:[aaaa valueForKey:@"OrgCode"]]) {
                [tttttt replaceObjectAtIndex:i withObject:aaaa];
            }
        }
        
        _dataArray = tttttt;
    }
    [tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
